<template>
<div class="login_container">
  <div class="login_box">
<!--    //登录头像区域-->
    <div class="avatar_box">
      <img src="../assets/logo.png" alt="">
    </div>
<!--    //登录表单区域-->
<!--    label_width是item中label的宽度-->
    <el-form
        ref="LoginFormRef"
        :rules="LoginFormRules"
        :model="LoginForm" label-width="0px" class="login_form">
<!--      用户名-->
      <el-form-item prop="username">
        <el-input  v-model="LoginForm.username" prefix-icon="iconfont icon-user">

        </el-input>
      </el-form-item>
<!--      密码-->
      <el-form-item prop="password">
        <el-input

            v-model="LoginForm.password"
            type="password"
            prefix-icon="iconfont icon-3702mima">
        </el-input>
      </el-form-item>
<!--      按钮区域-->
      <el-form-item class="btns">
        <el-button @click="login" type="primary">
          登录
        </el-button>
        <el-button type="info" @click="resetLoginForm">
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
export default {
  name: "Login",
  data(){
    return{
      LoginForm:{
        username:"admin",
        password:"123456"
      },
      LoginFormRules:{
        //username校验规则
        username:[
          {required:true,message:'请输入用户名',trigger:'blur'},
          {min:3,max:8,message: '长度在 3到8个字符',trigger:'blur'}
        ],
        password: [
          {required:true,message:'请输入密码',trigger:'blur'},
          {min:6,max:12,message: '长度在 6到12个字符',trigger:'blur'}
        ],
      },
    }
  },
  methods:{
    login(){
      //表单数据的预验证
      this.$refs.LoginFormRef.validate(async valid =>{
        if(!valid) return
        const {data: result} =await this.$axios.post("login",this.LoginForm)
        console.log(result)
        if(result.meta.status!==200)
          return this.$message.error('登陆失败')
        //登录成功之后保存token到sessionStorage
        window.sessionStorage.setItem('token',result.data.token)
        //页面跳转
        this.$router.push('/home')
      })
    },
    //重置表单
    resetLoginForm(){
      //console.log(this)
      this.$refs.LoginFormRef.resetFields()
    }
  }
}
</script>

<style  lang="less" scoped>
.login_container{
  background-color: #2b4b6b;
  height: 100%;
}
.login_box{
  width: 450px;
  height: 300px;
  background-color: #fff;
  //圆角
  border-radius: 3px;
  //位置设置 实现居中
  ///左上角移动到屏幕中间
  position: absolute;
  left: 50%;
  top:50%;
  ///向左上各移动自身的一半 实现居中
  transform:translate(-50%,-50%);
}
.avatar_box{
  width: 130px;
  height: 130px;
  //less %相对于父亲
  border: 1px solid #eee;
  //圆形
  border-radius: 50%;
  padding: 10px;
  //阴影
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%,-50%);
  background-color: #fff;
  img{
    height: 100%;
    width: 100%;
    border-radius: 50%;
    background-color: #eee;

  }
}
.btns{
  //居右对齐
  display: flex;
  justify-content: flex-end;
}
.login_form{
  position: absolute;
  bottom: 0;
  width: 100% ;
  padding: 0 21px;
  box-sizing: border-box;
}
</style>